#include "test_task.h"
#include "cmsis_os.h"
#include "bsp_buzzer.h"
#include "detect_task.h"

static void buzzer_warn_error(uint8_t num);

const error_t *error_list_test_local;

void test_task(void const *argument)
{
    static uint8_t error, last_error;
    static uint8_t error_num;
    error_list_test_local = get_error_list_point();

    while (1)
    {
        error = 0;

        for (error_num = 0; error_num < REFEREE_TOE; error_num++)
        {
            if (error_list_test_local[error_num].error_exist)
            {
                error = 1;
                break;
            }
        }

        if (error == 0 && last_error != 0)
        {
            buzzer_off();
        }

        if (error)
        {
            buzzer_warn_error(error_num + 1);
        }

        last_error = error;
        osDelay(10);
    }
}

static void buzzer_warn_error(uint8_t num)
{
    static uint8_t show_num = 0;
    static uint8_t stop_num = 100;
    if (show_num == 0 && stop_num == 0)
    {
        show_num = num;
        stop_num = 100;
    }
    else if (show_num == 0)
    {
        stop_num--;
        buzzer_off();
    }
    else
    {
        static uint8_t tick = 0;
        tick++;
        if (tick < 50)
        {
            buzzer_off();
        }
        else if (tick < 100)
        {
            buzzer_on(1, 30000);
        }
        else
        {
            tick = 0;
            show_num--;
        }
    }
}

void playStartupSound(void)
{
    buzzer_on(3, 2000);
    HAL_Delay(300);
    buzzer_off();
    buzzer_on(2, 2000);
    HAL_Delay(300);
    buzzer_off();
    buzzer_on(1, 2000);
    HAL_Delay(300);
    buzzer_off();
    buzzer_on(0, 2000);
    HAL_Delay(300);
    buzzer_off();
}

//�͵�������
//void low_power_warning()
//{
//    buzzer_on(1, 30000);
//    osDelay(500);
//    buzzer_off();
//    osDelay(500);
//    buzzer_on(0, 30000);
//    osDelay(500);
//    buzzer_off();
//    osDelay(500);
//}
